my_list = [3, "apple", 9, "banana", 7, "cherry", 2, "date", 5, "elderberry"]

my_list.append("fig")
print(my_list)
# my_list → [3, "apple", 9, "banana", 7, "cherry", 2, "date", 5, "elderberry", "fig"]

my_list.remove("banana")
print(my_list)
# my_list → [3, "apple", 9, 7, "cherry", 2, "date", 5, "elderberry", "fig"]

index = my_list.index(7)  # 找到7的索引
my_list[index] = "grape"
print(my_list)
# my_list → [3, "apple", 9, "grape", "cherry", 2, "date", 5, "elderberry", "fig"]

cherry_index = my_list.index("cherry")
print(f"Index of 'cherry': {cherry_index}")  # 输出: Index of 'cherry': 4

for item in my_list:
    print(item)
# 输出:
# 3
# apple
# 9
# grape
# cherry
# 2
# date
# 5
# elderberry
# fig

insert_index = my_list.index("cherry") + 1
my_list.insert(insert_index, "kiwi")
print(my_list)
# my_list → [3, "apple", 9, "grape", "cherry", "kiwi", 2, "date", 5, "elderberry", "fig"]

print(f"打印倒数第二个元素: {my_list[2]}")  # 输出: 打印倒数第二个元素: 9

sublist = my_list[2:7]
print(f"切片获取第3到第7个元素: {sublist}")  # 输出: 切片获取第3到第7个元素: [9, 'grape', 'cherry', 'kiwi', 2]

reversed_list = my_list[::-1]
print(f"反转列表: {reversed_list}")
# 输出:  反转列表: ['fig', 'elderberry', 5, 'date', 2, 'kiwi', 'cherry', 'grape', 9, 'apple', 3]



# 分离数字和字符串
numbers = [(i, x) for i, x in enumerate(my_list) if isinstance(x, int)]
strings = [x for x in my_list if isinstance(x, str)]
strings_sorted = sorted(strings)  # 排序字符串
# 重构列表
new_list = []
num_ptr = 0
str_ptr = 0
for i in range(len(my_list)):
    if num_ptr < len(numbers) and i == numbers[num_ptr][0]:
        new_list.append(numbers[num_ptr][1])
        num_ptr += 1
    else:
        new_list.append(strings_sorted[str_ptr])
        str_ptr += 1
my_list = new_list
print(f"仅对字符串排序: {my_list}")



number_words = {1: 'one', 2: 'two', 3: 'three', 5: 'five', 7: 'seven', 9: 'nine'}
for i in range(len(my_list)):
    if isinstance(my_list[i], int) and my_list[i] in number_words:
        my_list[i] = number_words[my_list[i]]
print(f"将数字替换为英文单词: {my_list}")
